var grid;
function initGrid(){	
	grid = $('#maingrid').ligerGrid({
		url:HTR.U('index/interest/pageQuery'),
		pageSize:HTR.pageSize,
		pageSizeOptions:HTR.pageSizeOptions,
		height:'99%',
        width:'100%',
        minColToggle:6,
        rownumbers:true,
        columns: [
	        { display: '编号', name: 'interestId',width: 100,isSort: false},
			{ display: '兴趣名称', name: 'interestName', isSort: false},
			{ display: '排序', name: 'interestSort', isSort: false},
	        { display: '操作', name: 'op',width:200,isSort: false,
	        	render: function (rowdata){
		            var h = "";
		            if(HTR.GRANT.XQAH_02)h += "<a href='javascript:toEdit("+rowdata["interestId"]+")'>修改</a> ";
		            if(HTR.GRANT.XQAH_03)h += "<a href='javascript:toDel("+rowdata["interestId"]+")'>删除</a> ";
		            return h;
	        	}}
        ]
    });
}

function toEdit(id){
	$('#interestForm')[0].reset();
	if(id>0){
		$.post(HTR.U('index/interest/get'),{id:id},function(data,textStatus){
			var json = HTR.toIndexJson(data);
			if(json){
				HTR.setValues(json);
				editsBox(id);
			}
		});
	}else{
		HTR.setValues({interestName:''});
		editsBox(id);
	}
}

function editsBox(id){
	var title =(id>0)?"修改兴趣爱好":"新增兴趣爱好";
	var box = HTR.open({title:title,type:1,content:$('#interestBox'),area: ['400px', '210px'],btn:['确定','取消'],yes:function(){
		$('#interestForm').submit();
	}});
	$('#interestForm').validator({
	    fields: {
	    	interestName: {
	    		tip: "请输入兴趣名称",
	    		rule: '兴趣名称:required;length[~25];'
	    	},
	    },
	    valid: function(form){
        var params = HTR.getParams('.ipt');
        params.interestId = id;
        var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
    		$.post(HTR.U('index/interest/'+((id>0)?"edit":"add")),params,function(data,textStatus){
    			layer.close(loading);
    			var json = HTR.toIndexJson(data);
    			if(json.status=='1'){
			    	HTR.msg(json.msg,{icon:1});
			    	layer.close(box);
			    	grid.reload();
    			}else{
    			    HTR.msg(json.msg,{icon:2});
    			}
    		});
	    }
	});
}

function toDel(id){
	var box = HTR.confirm({content:"您确定要删除该爱好吗?",yes:function(){
		var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
		$.post(HTR.U('index/interest/del'),{id:id},function(data,textStatus){
			layer.close(loading);
			var json = HTR.toIndexJson(data);
			if(json.status=='1'){
				HTR.msg(json.msg,{icon:1});
				layer.close(box);
				grid.reload();
			}else{
				HTR.msg(json.msg,{icon:2});
			}
		});
	}});
}